﻿#region File Description
//-----------------------------------------------------------------------------
// GameplayScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using FarseerPhysics.Dynamics;
using FarseerPhysics;
using Microsoft.Xna.Framework.Input.Touch;
using System.Diagnostics;
using GameName1.Programming;
using System.Windows;
using Microsoft.Phone.Controls;
#endregion

namespace GameName1
{
    /// <summary>
    /// This screen implements the actual game logic. It is just a
    /// placeholder to get the idea across: you'll probably want to
    /// put some more interesting gameplay in here!
    /// </summary>
    class BattleScreen : GameScreen
    {
        #region Fields

        ContentManager content;

        World world = new World(new Vector2(0f, 0f));
        Robot robot1;
        Robot robot2;
        Obstacle obstacle;
        BrickWall wall;
        Arena arena;

        Random random = new Random();



        //Programmer programmer;

        const int buttonHeight = 120;
        const int buttonWidth = 120;
        const int buttonMargin = 15; 

        float pauseAlpha;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public BattleScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);


        }


        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            //gameFont = content.Load<SpriteFont>("gamefont");
            //programmer = new Programmer();
            ConvertUnits.SetDisplayUnitToSimUnitRatio(64f);

            robot1 = new Robot(ScreenManager.Game, world, "chassis1", new Vector2(3f, 3f));
            robot2 = new Robot(ScreenManager.Game, world, "chassis1", new Vector2(3f, 12f));
            obstacle = new Obstacle(ScreenManager.Game, world, "obstacle1", new Vector2(6f, 6f));
            wall = new BrickWall(ScreenManager.Game, world, "brick1", new Vector2(0f, 0f));
            arena = new Arena(ScreenManager.Game, world, new Vector2(6f, 6f));
            robot1.Name = "robot1";
            robot2.Name = "robot2";
            robot1.Arena = arena;
            robot2.Arena = arena;
            robot1.Code = ProgrammingPage.UserCode;
            AddGameComponent(arena);
            //AddGameComponent(button1);
            //AddGameComponent(button2); 
            AddGameComponent(robot1);
            AddGameComponent(robot2);
            AddGameComponent(obstacle);

            robot1.TurnOn();
            // once the load has finished, we use ResetElapsedTime to tell the game's
            // timing mechanism that we have just finished a very long frame, and that
            // it should not try to catch up.
            ScreenManager.Game.ResetElapsedTime();
        }


        /// <summary>
        /// Unload graphics content used by the game.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }


        #endregion

        #region Update and Draw


        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (coveredByOtherScreen)
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            else
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);

            if (IsActive)
            {
                robot1.Update(gameTime);
                robot2.Update(gameTime);
                world.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);
            }
        }


        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay screen is active.
        /// </summary>
        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);
            if (input == null)
                throw new ArgumentNullException("input");

            TouchCollection touches = input.TouchState;

            if (touches.Count == 1)
            {
                foreach (TouchLocation tl in touches)
                {
                    //Vector2 forcePixel = tl.Position - leftWheelPosition;
                    //Vector2 forcePixel = tl.Position - chassisPosition;
                   // Vector2 force = ConvertUnits.ToSimUnits(forcePixel);

                    //DrawLine(batch, tl.Position, leftWheelPosition, Color.Black, 1);
                    //_chassis.ApplyForce(force * 8, _chassis.Position);
                    //_leftWheel.ApplyForce(force * 8, _leftWheel.Position);

                }



            }
        }


        /// <summary>
        /// Draws the gameplay screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            // This game has a blue background. Why? Because!
            

            // Our player and enemy are both actually just text strings.
           /* SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin();

            wall.Draw(gameTime, spriteBatch);
            arena.Draw(gameTime, spriteBatch);
            robot1.Draw(gameTime, spriteBatch);
            robot2.Draw(gameTime, spriteBatch);
            obstacle.Draw(gameTime, spriteBatch);

            button1.Draw(gameTime, spriteBatch);
            button2.Draw(gameTime, spriteBatch);
            spriteBatch.End();*/

            // If the game is transitioning on or off, fade it out to black.
            if (TransitionPosition > 0 || pauseAlpha > 0)
            {
                float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);

                ScreenManager.FadeBackBufferToBlack(alpha);
            }
        }


        #endregion
    }
}

